<script setup name="DocProcessView">
import {useRoute} from "vue-router"
import {getDocumentOperateMenu} from "@/utils/menu.js";
import {getDocStatusStyle} from "@/utils/document.js";
import Steps from "@/views/common/steps.vue";
import AuditView from "@/views/document/process/processing/flowInfo/audit/auditView.vue";
import DocumentView from "@/views/document/common/basicInfo/documentView.vue";
import {getButtons, getDocProcessBasicInfo} from "@/api/document/processDocument.js";
import JointSignView from "@/views/document/process/processing/flowInfo/jointSign/jointSignView.vue";
import LeaderSignView from "@/views/document/process/processing/flowInfo/leaderSign/leaderSignView.vue";
import AttachmentList from "@/views/common/attachment/attachmentList.vue";
import SignView from "@/views/document/process/processing/flowInfo/sign/signView.vue";
import {
  getDocProcessAttachmentFile,
  getDocProcessAttachmentPageList
} from "@/api/document/attachment.js";
import {tabFunctions} from "@/utils/tab.js";
import FlowInfoView from "@/views/document/process/processed/flowInfo/flowInfoView.vue";

const {proxy} = getCurrentInstance()
const {docStatus} = proxy.useDict(["docStatus", false])
const activeTabName = ref('flowInfo')
const route = useRoute()
const docId = route.params && route.params.docId
const listStatus = route.query && route.query.status
const documentInfo = ref({})
const opMenuEnum = getDocumentOperateMenu()
provide('currentMenu', opMenuEnum.process)
const data = reactive({
  flowInfoButtons: {
    isAudit: false,
    isJointSign: false,
    isLeaderSign: false,
    isSign: false,
    isBack: false,
    isOpenFinalPdf: false,
  },
  attachmentOpInfo: {
    listQueryParams: {
      docId: docId
    },
    backListUrl: '/document/' + opMenuEnum.process,
    openUrl: '/document/attachment/openFile/',
    getFileApi: getDocProcessAttachmentFile,
    getListApi: getDocProcessAttachmentPageList
  },
  isRefreshAttachment: false,
  isRefreshAdvice: false
})
const {flowInfoButtons, attachmentOpInfo, isRefreshAttachment, isRefreshAdvice} = toRefs(data)
const {handChangeTab} = tabFunctions(activeTabName)

onMounted(() => {
  loadMainViewData()
})

function loadMainViewData() {
  if (docId) {
    getDocBasicInfo()
    getButtonsInfo()
  }
}

function getDocBasicInfo() {
  getDocProcessBasicInfo(docId).then(res => {
    documentInfo.value = res
  })
}

function getButtonsInfo() {
  getButtons(docId).then(res => {
    flowInfoButtons.value = res
  })
}

const handClickTab = (tab, event) => {
  //实时刷新页面数据
  loadMainViewData()
  if ('flowInfo' === tab.props.name) {
    isRefreshAdvice.value = true
  } else if ('attachment' === tab.props.name) {
    isRefreshAttachment.value = true
  }
  setTimeout(() => {
    setRefreshData()
  },50)
}

const setRefreshData = () => {
  isRefreshAttachment.value = false
  isRefreshAdvice.value = false
}

</script>

<template>
  <div class="app-container">
    <el-card :span="24" class="mb10">
      <Steps :status="documentInfo.status" :statusSteps="docStatus.filter(item => item.value !== '已废弃')"/>
    </el-card>
    <el-card :span="24">
      <template #header>
        <el-row style="line-height: 40px">
          <el-text type="primary" style="font-size: 18px;">{{ documentInfo.subject }}</el-text>
          <el-text class="ml10">
            <el-tag :style="getDocStatusStyle(documentInfo.status)">状态：{{documentInfo.status}}</el-tag>
            <el-tag :style="getDocStatusStyle(documentInfo.status)" effect="plain" class="ml10">拟稿人：{{ documentInfo.userName }}</el-tag>
            <el-tag type="info" effect="plain" class="ml10">新建时间：{{ documentInfo.createTime }}</el-tag>
            <el-tag type="info" effect="plain" class="ml10" v-if="documentInfo.updateTime!==null">更新时间：{{ documentInfo.updateTime }}</el-tag>
          </el-text>
        </el-row>
      </template>
      <el-row>
        <div class="el-table el-table--enable-row-hover el-table--medium">
          <el-tabs v-model="activeTabName" @tab-change="handChangeTab" @tab-click="handClickTab">
            <el-tab-pane label="流程信息" name="flowInfo" lazy>
              <FlowInfoView :docId="documentInfo.id" :isOpenFinalPdf="flowInfoButtons.isOpenFinalPdf" :isRefreshAdvice="isRefreshAdvice"
                           v-if="listStatus==='已办'"/>
              <AuditView :documentInfo="documentInfo" :flowInfoButtons="flowInfoButtons" :isRefreshAdvice="isRefreshAdvice"
                         v-else-if="documentInfo.status==='审核修订'"/>
              <JointSignView :documentInfo="documentInfo" :flowInfoButtons="flowInfoButtons" :isRefreshAdvice="isRefreshAdvice"
                             v-else-if="documentInfo.status==='会签'"/>
              <LeaderSignView :documentInfo="documentInfo" :flowInfoButtons="flowInfoButtons" :isRefreshAdvice="isRefreshAdvice"
                              v-else-if="documentInfo.status==='领导签发'"/>
              <SignView :documentInfo="documentInfo" :flowInfoButtons="flowInfoButtons" :isRefreshAdvice="isRefreshAdvice"
                        v-else-if="documentInfo.status==='签章'"/>
            </el-tab-pane>
            <el-tab-pane label="基本信息" name="documentBasicInfo" lazy>
              <DocumentView :documentInfo="documentInfo"/>
            </el-tab-pane>
            <el-tab-pane label="公文附件" name="attachment" lazy>
              <AttachmentList :attachmentOpInfo="attachmentOpInfo" :isRefreshAttachment="isRefreshAttachment"/>
            </el-tab-pane>
          </el-tabs>
        </div>
      </el-row>
    </el-card>
  </div>
</template>
